/**
	Copyright (c) 2009 Ozazo Pvt Ltd, INDIA

	Permission is hereby granted, free of charge, to any person or organization 
	obtaining a copy of this software and associated  documentation  files (the 
	"Software"), to deal in the Software without restriction, including without 
	limitation the rights to use, copy, modify, merge, publish, distribute, sub
	license, and/or sell copies of the Software, and to permit persons  to whom 
	the Software is furnished to do so, subject to the following conditions:
	
	The above copyright notice and  this permission notice shall be included in 
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS",  WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING  BUT NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY,  WHETHER  IN  AN  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
	FROM,  OUT  OF  OR  IN  CONNECTION  WITH  THE  SOFTWARE OR THE USE OR OTHER 
	DEALINGS IN THE SOFTWARE
 */

package com.ozazo.wave.robots.fw.search;

import java.util.*;


/**
 * 
 * @author Araf Karsh Hamid
 * @version 1.0
 * @seriel 20091121102030
 *
 */
public class OpenSearchResult {

	private final String queryString;
	private List<OpenSearchItem> list;

	public OpenSearchResult(final String _query) {
		queryString = _query;
		list = new ArrayList<OpenSearchItem>();
	}

	public void addItem(OpenSearchItem _item) {
		if(_item != null) {
			list.add(_item);
		}
	}

	public void clear() {
		list.clear();
	}

	public List getAllRecords() {
		return list;
	}

	public String getQueryString() {
		return queryString;
	}

	public int hashCode() {
		return queryString.hashCode();
	}

	public String toString() {
		return queryString;
	}

	public boolean equals(Object o) {
		try {
			OpenSearchResult w = (OpenSearchResult)o;
			return (w.queryString.equals(this.queryString)) ? true : false;
		} catch (Exception ignored) {}
		return false;
	}

	public boolean isResultAvailable() {
		return (list.size() > 0) ? true : false;
	}

	public OpenSearchItem getFirstResult() {
		return list.get(0);
	}

	public String toXMLString() {
		StringBuilder buf = new StringBuilder();
		buf.append("<?xml version=\"1.0\"?>\n");
		buf.append("<SearchSuggestion version=\"2.0\" xmlns=\"http://opensearch.org/searchsuggest2\">\n");
		buf.append("\t<Query xml:space=\"preserve\">").append(queryString).append("</Query>\n");
		buf.append("\t<Section>\n");
		for(OpenSearchItem w : list) {
			buf.append(w.toXMLString());
		}
		buf.append("\t</Section>\n");
		buf.append("</SearchSuggestion>");
		return buf.toString();
	}

}
